﻿#region Using Directives

using EnvDTE;
using System;
using System.Text;
using System.IO;
using EnvDTE80;
using System.ComponentModel;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.TeamArchitect.PowerTools.Features;
using Microsoft.VisualStudio.Shell;

#endregion

namespace FeatureBuilder.ContribLib.ValueProviders
{
    /// <summary>
    /// Returns the name of the active solution
    /// </summary>
    [DisplayName("SolutionNameProvider")]
    [Description("Returns the name of the active Solution")]
    [Category("IDE")]
    public class SolutionNameProvider : ValueProvider
    {
        /// <summary>
        /// Gets the ServiceProvider that we will use to get a reference to DTE.
        /// </summary>
        [Import(typeof(SVsServiceProvider))]
        public IServiceProvider ServiceProvider { get; private set; }

        #region Overrides

        /// <summary>
        /// Returns the Solution Name
        /// </summary>
        public override object Evaluate()
        {
            return GetSolutionName();
        }

        private string GetSolutionName()
        {
            string solutionName = null;
            DTE solutionDTE = this.ServiceProvider.GetService(typeof(DTE)) as DTE;

            if (solutionDTE != null)
            {
                try
                {
                    Property property = solutionDTE.Solution.Properties.Item("Name");
                    solutionName = property.Value.ToString();
                }
                catch
                {
                    solutionName = null;
                }
            }
            return solutionName;
        }

        #endregion
    }
}

